package org.hismig.migration;

import org.hismig.cfg.Configuration;
import org.hismig.report.JobResultReport;
import org.hismig.scheduler.ITrigger;
import org.hismig.utils.StringTool;
import org.hismig.view.MainFrame;

/**
 * @declare: The HisMigConsole<br />
 *           This class created by Vansion.Hopihit on 2012-07-04 <br />
 *           <a href="mailto:vansionhopihit@gmail.com">Contact Me</a>
 * @version 1.0
 * @author Vansion.Hopihit
 * 
 */
public class HisMigConsole {

	private static final String CFG_FILE_NAME = "HisMig.cfg.xml";

	private static HisMigConsole hisMigConsole;

	private String fileName;

	private HisMigConsole() {
	}

	public static HisMigConsole getInstance() {

		if (null == hisMigConsole) {
			hisMigConsole = new HisMigConsole();
		}

		return hisMigConsole;
	}

	public void execute(String fileName) {

		this.fileName = fileName;
		Configuration configuration = new Configuration(getFileName());

		Constructor constructor = new Constructor();
		constructor.init(configuration);
		ITrigger trigger = constructor.getTrigger();

		MainFrame testJFrame = new MainFrame(trigger);

		testJFrame.start();
		trigger.addListener(testJFrame);
		trigger.addListener(new JobResultReport());

		trigger.execute(configuration);
	}

	public static void main(String[] args) {

		String fileName = null;

		if (0 < args.length) {
			fileName = args[0];
		}

		HisMigConsole.getInstance().execute(fileName);

	}

	public String getFileName() {
		if (StringTool.isEmpty(fileName)) {
			return CFG_FILE_NAME;
		}

		return fileName;
	}
}
